package sketch.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Vector;

import javax.swing.JPanel;

import sketch.doc.usketch.Document;
import sketch.doc.usketch.Stroke;
import sketch.earlyproc.EarlyProcessor;
import sketch.geo.gPoint;
import sketch.geo.gPoints;
import sketch.labels.Tree;
import sketch.labels.nodes.RootNode;



public class SketchPanel extends JPanel {
	
	public static final boolean DEBUG = true;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	SketchFrame frame;

	
	private boolean drawable = true;
	private int initial_height;
	
	public void setDrawable(boolean b) {
		drawable = b;
	}
	
	Vector<gPoint> ink;
	gPoint prev;
	Document document;

	/**
	 *author : hr 
	 */
	//manage labelsTree
	private Tree tree=null;
	
	/**
	 * This is the default constructor
	 */
	public SketchPanel(SketchFrame owner, Document sketch) {
		super();
		initialize();
		document = sketch;
		frame = owner;
		
		tree=getTree();
		new NodeLabelsFrame(getThis()).setVisible(true);
	}
	
	private SketchPanel getThis() {
		return this;
	}
	
	public Tree getTree(){
		if(tree==null){
			tree=new Tree();
		}
		return tree;
	}
	
	
	public void clear() {
		document.clear();
		EarlyProcessor.clear();
		
		tree.clear();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize(){
//		this.setSize(300, 200);
		ink = new Vector<gPoint>();
		initial_height = 0;
		this.setBackground(java.awt.Color.white);

		this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
			public void mouseDragged(java.awt.event.MouseEvent e) {
				//System.out.println("mouseDragged()");
				if (!drawable)
					return;
				
				gPoint p = new gPoint(e.getX(), e.getY() ,e.getWhen());
				ink.add(p);
				if(e.getY()>getThis().getHeight()){
					int height = getThis().getHeight();
					
					if(initial_height==0) 
						initial_height=height;
					getThis().setPreferredSize(
							new Dimension(getThis().getWidth(),height+initial_height));
					frame.getScrollPane().getVerticalScrollBar().setValue(height);
					getThis().updateUI();				
				}
				//draw line between two points
				Graphics2D g2 = (Graphics2D) getThis().getGraphics();
				g2.drawLine(prev.X(), prev.Y(), p.X(), p.Y());
				prev = p;
			}
		});
		this.addMouseListener(new java.awt.event.MouseAdapter() {   
			public void mouseClicked(java.awt.event.MouseEvent e) { 
				if (!drawable)
					return;
				
				int count = e.getClickCount();
				if (count == 2) {

					gPoint choose = new gPoint(e.getX(), e.getY());
					int index = document.chooseStroke(choose);
					if(index>=0){
						Stroke stroke = document.get(index);
						boolean is = stroke.getResamplable();
						stroke.setResamplable(!is);
						document.setResampleNum(document.getResampleNum()+1);
						document.addSelectStroke(stroke);
						//document.setStrokeLabel(index);
						//document.setStrokeColor(index);
					}
				}
			}   
			
			public void mouseReleased(java.awt.event.MouseEvent e) {    
				//	System.out.println("mouseReleased()"); 
				if (!drawable)
					return;

				ink.add(new gPoint(e.getX(), e.getY() ,e.getWhen()));
				
				//System.out.println("release time: " +rDate.getTime());
				if (ink.size() > 2) {
					//add stroke
					Stroke stroke = document.createStroke();
					stroke.setPoints(new gPoints(ink));
					
					/*TODO: segment
					EarlyProcessor ep = EarlyProcessor.getInstance();
					ep.setStroke(stroke);
					ep.process(); 
					
					HMMSegmentor hmmseg = HMMSegmentor.getInstance();
					hmmseg.setStroke(stroke);
					hmmseg.process();	
					*/				
				}
				ink = new Vector<gPoint>();
				getThis().repaint();
							
//				if (DEBUG)
//					System.out.println(document.toString());
			}
			public void mousePressed(java.awt.event.MouseEvent e) {
				if (!drawable)
					return;
				prev = new gPoint(e.getX(), e.getY() ,e.getWhen());
				ink.add(prev);
				//System.out.println("start draw time: " + dPress.getTime());
			}
		});
	}
	
	public void paintComponent(Graphics g) {
		//System.out.println(document.getSelectStroke().size());
		super.paintComponent(g); 
		Graphics2D g2 = (Graphics2D) g;
		document.draw(g2);	          //draw all of the strokes
		
//		if(initial_height ==0)
//			initial_height = this.getHeight();

	//	this.getParent().repaint();
	}

}
